package com.tojabo.swing.twelve_window;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class JOptionPaneDemo extends JPanel {

	private static final long serialVersionUID = 1L;

	static final int WIDTH = 700;
	static final int HEIGHT = 400;

	final JFrame f;

	JOptionPaneDemo() {
		f = new JFrame("对话框测试窗口");
		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		f.setLocation(x, y);
		f.setSize(WIDTH, HEIGHT);
		f.setContentPane(this);
		
		setLayout(new FlowLayout());
		
		try {
			// 将LookAndFeel设置成Windows样式
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");			
		} catch (Exception ex) {
			ex.printStackTrace();
		}


		JButton b1 = new JButton("showMessageDialog");// 创建四个按钮组件，这四个按钮组件引发的动作事件是显示出四个不同类型的对话框
		JButton b2 = new JButton("showConfirmDialog");
		JButton b3 = new JButton("showOptionDialog");
		JButton b4 = new JButton("showInputDialog");
		add(b1);
		add(b2);
		add(b3);
		add(b4);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				JOptionPane.showMessageDialog(null, "这是一个消息对话框");// 这是一个showMessageDialog静态方法显示的对话框
			}
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				JOptionPane.showConfirmDialog(null, "这是一个有关confirm的消息",
						"这是一个有关confirm的对话框", JOptionPane.YES_NO_CANCEL_OPTION); // 这是一个showConfirmDialog静态方法显示的对话框
			}
		});
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				but3_Action(Event);								

			}// 这是一个showOptionDialog静态方法显示的对话框
		});
		b4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				Object[] options = { "第一", "第二", "第三" };
				JOptionPane.showInputDialog(null, "请选择一个", "输入",
						JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
								"blue.gif"), options, options[0]);
			}// 这是一个showInputDialog静态方法显示的对话框
		});

		f.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				close_Action(e);
			}
		});
		
		
		f.setVisible(true);
	}

	protected void but3_Action(ActionEvent event) {
		Object[] options = { "确定", "取消" };
		int select = JOptionPane.showOptionDialog(null, "保存成功,是否打开文件夹", "提示",
				JOptionPane.DEFAULT_OPTION,
				JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
				
		
		if(select ==0){
			try {
				java.awt.Desktop.getDesktop().open(new File("E:\\"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	protected void close_Action(WindowEvent e) {
		int option = JOptionPane.showConfirmDialog(f, "确定退出系统? ", "提示 ",
				JOptionPane.YES_NO_CANCEL_OPTION);
		if (option == JOptionPane.YES_OPTION) {			
			System.exit(0);			
		}else if(option == JOptionPane.NO_OPTION){
			return ;
		}else if(option == JOptionPane.CANCEL_OPTION){
			return ;
		}		
	}
	
	

	public static void main(String[] args) {
		new JOptionPaneDemo();
	}

}
